﻿using System;
using System.Windows;
using System.Windows.Controls;
using Catel.Articles.Base.UI.Windows;

namespace Catel.Articles.Base.UI
{
    /// <summary>
    /// Example helper.
    /// </summary>
    public static class ExampleHelper
    {
        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement)
        {
            ShowControlInWindow(frameworkElement, null);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText)
        {
            ShowControlInWindow(frameworkElement, labelText, 0, 0);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <param name="minWidth">Minimum width of the control. If smaller or equal to 0, no minimum width is specified.</param>
        /// <param name="maxWidth">Maximum width of the control. If smaller or equal to 0, no maximum width is specified.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText, int minWidth, int maxWidth)
        {
            ShowControlInWindow(frameworkElement, labelText, minWidth, maxWidth, 0, 0);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <param name="minWidth">Minimum width of the control. If smaller or equal to 0, no minimum width is specified.</param>
        /// <param name="maxWidth">Maximum width of the control. If smaller or equal to 0, no maximum width is specified.</param>
        /// <param name="minHeight">Minimum height of the control. If smaller or equal to 0, no minimum height is specified.</param>
        /// <param name="maxHeight">Maximum height of the control. If smaller or equal to 0, no maximum height is specified.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText, int minWidth, int maxWidth, int minHeight, int maxHeight)
        {
            Argument.IsNotNull("frameworkElement", frameworkElement);

            // Create container
            Grid container = new Grid();

            // Row definitions
            RowDefinition firstRow = new RowDefinition();
            firstRow.Height = new GridLength(1, GridUnitType.Auto);
            if (minHeight > 0)
            {
                firstRow.MinHeight = minHeight;
            }
            if (maxHeight > 0)
            {
                firstRow.MaxHeight = maxHeight;
            }
            container.RowDefinitions.Add(firstRow);

            // Column definitions
            ColumnDefinition firstColumn = new ColumnDefinition();
            firstColumn.Width = new GridLength(1, GridUnitType.Auto);
            container.ColumnDefinitions.Add(firstColumn);
            ColumnDefinition secondColumn = new ColumnDefinition();
            firstColumn.Width = new GridLength(1, GridUnitType.Star);
            if (minWidth > 0)
            {
                secondColumn.MinWidth = minWidth;
            }
            if (maxWidth > 0)
            {
                secondColumn.MaxWidth = maxWidth;
            }
            container.ColumnDefinitions.Add(secondColumn);

            // Should we add a label?);
            if (!string.IsNullOrEmpty(labelText))
            {
                // Yes, add it
                Label label = new Label();
                label.Content = labelText;
                label.SetValue(Grid.ColumnProperty, 0);
                container.Children.Add(label);
            }

            // Add control
            frameworkElement.SetValue(Grid.ColumnProperty, 1);
            container.Children.Add(frameworkElement);

            // Show container in window
            ControlExampleWindow controlExampleWindow = new ControlExampleWindow();
            controlExampleWindow.LoadFrameworkElement(container);

            // Show window
            controlExampleWindow.ShowDialog();
        }
    }
}
